package com.jjsc.client.controller;

import com.jjsc.client.api.domain.UserAddress;
import com.jjsc.client.service.IUserAddressService;
import com.jjsc.common.core.context.SecurityContextHolder;
import com.jjsc.common.core.domain.R;
import com.jjsc.common.core.web.controller.BaseController;
import com.jjsc.common.core.web.domain.AjaxResult;
import com.jjsc.common.security.annotation.InnerAuth;
import com.jjsc.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 钟城明
 * @CreateTime: 2025-10-23
 * @Description: 用户地址管理
 * @Version: 1.0
 */
@Tag(name = "用户地址管理")
@RestController
@RequestMapping("/userAddress")
public class UserAddressController extends BaseController {
    @Autowired
    private IUserAddressService userAddressService;

    /**
     * 查询用户地址列表
     * @return 用户地址列表
     */
    @Operation(summary = "查询用户地址列表")
    @GetMapping("/list")
    public AjaxResult list() {
        Long userId = SecurityContextHolder.getUserId();
        List<UserAddress> list = userAddressService.selectUserAddressList(userId);
        return success(list);
    }

    /**
     * 添加用户地址
     *
     * @param userAddress 用户地址
     * @return 添加结果
     */
    @Operation(summary = "添加用户地址")
    @PostMapping
    public AjaxResult add(@RequestBody UserAddress userAddress) {
        return toAjax(userAddressService.addUserAddress(userAddress));
    }

    /**
     * 修改用户地址
     *
     * @param userAddress 用户地址
     * @return 修改结果
     */
    @Operation(summary = "修改用户地址")
    @PutMapping
    public AjaxResult edit(@RequestBody UserAddress userAddress) {
        return toAjax(userAddressService.updateUserAddress(userAddress));
    }

    /**
     * 删除用户地址
     *
     * @param id 用户地址ID
     * @return 删除结果
     */
    @Operation(summary = "删除用户地址")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        return toAjax(userAddressService.removeById(id));
    }
    /**
     * 远程调用
     * 根据ID查询用户地址
     * @param id 用户地址ID
     * @return 用户地址
     */
    @InnerAuth
    @GetMapping(value = "/getUserAddress/{id}")
    public R<UserAddress> getUserAddress(@PathVariable("id") Long id) {
        return R.ok(userAddressService.getById(id));
    }

}
